public class Demo {
    public static void main(String[] args) {
        Thread thread = new Thread(()->{
            // Thread.currentThread()作用是得到当前的线程
            // isInterrupted() 判断标识符是否为false
            //或者 while(!Thread.interrupted()) 效果一样
           while(!Thread.currentThread().isInterrupted()){
               System.out.println("线程工作中！");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();//打印异常
                   System.out.println("还需要完成的操作...");//2.
                   break;//1.直接退出
               }
           }
        });
        thread.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();//将标识符设置为true
    }
}
